class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        head =  node = ListNode()
        while l1 is not None or l2 is not None:
            if l1 is None:
                node.next = l2
                l2 = None
            elif l2 is None:
                node.next = l1
                l1 = None
            else:
                if l1.val< l2.val:
                    node.next = l1
                    l1 = l1.next
                    node = node.next
                else:
                    node.next = l2
                    l2 = l2.next
                    node = node.next
        return head.next